package com.lepu.flutterdemo

import android.app.Application
import android.content.Intent
import com.idlefish.flutterboost.FlutterBoost
import com.idlefish.flutterboost.FlutterBoostDelegate
import com.idlefish.flutterboost.FlutterBoostRouteOptions
import com.idlefish.flutterboost.containers.FlutterBoostActivity
import com.lepu.flutterdemo.boost.NativePageActivity
import io.flutter.embedding.android.FlutterActivityLaunchConfigs
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.FlutterEngineCache
import io.flutter.embedding.engine.dart.DartExecutor

/**
 * @author yangqing
 * @time 2021/10/15 9:29
 * @describe
 */
class MyApplication :Application() {
    override fun onCreate() {
        super.onCreate()
        // 引擎缓存
        val flutterEngineOne = FlutterEngine(this)
        flutterEngineOne.dartExecutor.executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault())
        FlutterEngineCache
            .getInstance()
            .put("my_engine_id", flutterEngineOne)

        val flutterEngineTwo= FlutterEngine(this)
        flutterEngineTwo.getNavigationChannel().setInitialRoute("test_page");
        flutterEngineTwo.dartExecutor.executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault())
        FlutterEngineCache
            .getInstance()
            .put("my_engine_id_two", flutterEngineTwo)

        //boot
        FlutterBoost.instance().setup(this, object : FlutterBoostDelegate {
            override fun pushNativeRoute(options: FlutterBoostRouteOptions) {
                var intent: Intent;
                if (options.pageName().equals("native_page_one")) {
                    val arguments = options.arguments()
                    val param = arguments.get("param") as String
                    intent = Intent(
                        FlutterBoost.instance().currentActivity(),
                        NativePageActivity::class.java
                    )
                    intent.putExtra("param", param)
                } else {
                    //这里根据options.pageName来判断你想跳转哪个页面，这里简单给一个
                    intent = Intent(
                        FlutterBoost.instance().currentActivity(),
                        MainActivity::class.java
                    )
                }
                FlutterBoost.instance().currentActivity()
                    .startActivityForResult(intent, options.requestCode())
            }

            override fun pushFlutterRoute(options: FlutterBoostRouteOptions) {
                val intent: Intent =
                    FlutterBoostActivity.CachedEngineIntentBuilder(FlutterBoostActivity::class.java)
                        .backgroundMode(FlutterActivityLaunchConfigs.BackgroundMode.transparent)
                        .destroyEngineWithActivity(false)
                        .uniqueId(options.uniqueId())
                        .url(options.pageName())
                        .urlParams(options.arguments())
                        .build(FlutterBoost.instance().currentActivity())
                FlutterBoost.instance().currentActivity().startActivity(intent)
            }
        }) { engine: FlutterEngine? -> }
    }
}